﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ECRMS.Common;
using ECRMS.Biz.Dom;
using ECRMS.Biz;
using System.Threading;
using System.Web.Security;
using System.ServiceModel;
using System.ServiceModel.Web;

namespace ECRMS.Service
{
    [ServiceContract]
    public partial interface IUsrService
    {
        [OperationContract]
        [return: MessageParameter(Name = "Result")]
        [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped,
            RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        ServiceResult Login(string username, string password);

        [OperationContract]
        [WebInvoke(Method = "POST")]
        void Logout();

        [OperationContract]
        List<EcUser> GetUsers();

        [OperationContract]
        [return: MessageParameter(Name = "Result")]
        [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped,
            RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        List<Employer> GetEmployers(Guid corporationRefId);

        [OperationContract]
        [return: MessageParameter(Name = "Result")]
        [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped,
            RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        ServiceResult ChangePassword(string oldPwd, string newPwd, string confirmPwd);
    }

    public partial class ECRService : IUsrService
    {
        ServiceResult IUsrService.Login(string username, string password)
        {
            EcUserLogic logic = new EcUserLogic();
            if (logic.TryLogin(username, password))
            {
                return new ServiceResult
                {
                    Status = 200
                };
            }

            return new ServiceResult
            {
                Status = 400
            };
        }

        void IUsrService.Logout()
        {
            EcUserLogic logic = new EcUserLogic();
            logic.Logout();
        }

        List<Employer> IUsrService.GetEmployers(Guid corporationRefId)
        {
            var logic = new EcUserLogic();
            return logic.GetEmployers(corporationRefId);
        }

        ServiceResult IUsrService.ChangePassword(string oldPwd, string newPwd, string confirmPwd)
        {
            Thread.Sleep(1000);

            return new ServiceResult
            {
                Status = 200
            };
        }


        List<EcUser> IUsrService.GetUsers()
        {
            var logic = new EcUserLogic();
            return logic.GetUsers();
        }
    }
}
